package com.xiaoma.chat.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dexcoder.assistant.persistence.Criteria;
import com.dexcoder.assistant.persistence.JdbcDao;
import com.xiaoma.chat.domain.ChatGroup;
import com.xiaoma.chat.domain.ChatRm;
import com.xiaoma.chat.domain.GroupUser;
import com.xiaoma.chat.vo.ChatGroupVo;
import com.xiaoma.chat.vo.UserVo;

@Service
public class GroupService {

    @Autowired
    private JdbcDao jdbcDao;
    
    public ChatGroup getById(int groupId) {
        return jdbcDao.get(ChatGroup.class, groupId);
    }
    
    public int insertGroup(ChatGroup group) {
        return jdbcDao.insert(group);
    }
    
    public ChatGroup findGroupByName(String name, int organizationId) {
        ChatGroup entity = new ChatGroup();
        entity.setName(name);
        entity.setOrganizationId(organizationId);
        return jdbcDao.querySingleResult(entity);
    }
    
    public void saveGroupUser(GroupUser groupUser) {
        jdbcDao.save(groupUser);
    }
    
    public List<UserVo> findUsersByGroupId(int groupId) {
        return jdbcDao.querySql(UserVo.class, "select u.*, b.role from user u left join group_user b on u.id = b.user_id where b.group_id = ?", new Object[]{groupId});
    }
    
    public GroupUser findGroupUser(int userId, int groupId) {
        GroupUser entity = new GroupUser();
        entity.setUserId(userId);
        entity.setGroupId(groupId);
        return jdbcDao.querySingleResult(entity);
    }
    
    public List<ChatGroupVo> findGroupUsers(int userId) {
        List<ChatGroup> groups = jdbcDao.querySql(ChatGroup.class, "select b.* from group_user a left join chat_group b on a.group_id = b.id where a.user_id = ? order by b.id desc", new Object[]{userId});
        List<ChatGroupVo> groupVos = new ArrayList<ChatGroupVo>(groups.size());
        for (ChatGroup group : groups) {
            ChatGroupVo groupVo = new ChatGroupVo();
            groupVo.build(group, this.findGroupUserCount(group.getId()));
            groupVos.add(groupVo);
        }
        return groupVos;
    }
    
    public int findGroupUserCount(int groupId) {
        GroupUser entity = new GroupUser();
        entity.setGroupId(groupId);
        return jdbcDao.queryCount(entity);
    }
    
    public List<ChatGroupVo> findGroupsByOrgId(int organizationId) {
        return jdbcDao.querySql(ChatGroupVo.class, "select a.*, count(b.id) as user_count from chat_group a left join group_user b on a.id = b.group_id where a.organization_id = ? group by a.id order by a.id desc", new Object[]{organizationId});
    }
    
    @Transactional
    public void destroyGroup(int groupId) {
        jdbcDao.delete(ChatGroup.class, groupId);
        Criteria criteria = Criteria.create(GroupUser.class).where("groupId", new Object[] {groupId});
        jdbcDao.delete(criteria);
        Criteria criteria1 = Criteria.create(ChatRm.class).where("groupId", new Object[] {groupId});
        jdbcDao.delete(criteria1);
    }
    @Transactional
    public void quitGroup(int groupId, List<Integer> userIds) {
        Criteria criteria = Criteria.create(GroupUser.class).where("groupId", new Object[] {groupId}).and("userId", userIds.toArray());
        jdbcDao.delete(criteria);
        Criteria criteria1 = Criteria.create(ChatRm.class).where("groupId", new Object[] {groupId}).and("student_id",userIds.toArray());
        jdbcDao.delete(criteria1);
        Criteria criteria2 = Criteria.create(ChatRm.class).where("groupId", new Object[] {groupId}).and("teacher_id", userIds.toArray());
        jdbcDao.delete(criteria2);
    }
}
